package com.example.mediamoudle.pickvideo

import android.graphics.*
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import com.bumptech.glide.load.resource.bitmap.TransformationUtils
import com.bumptech.glide.util.Util
import java.nio.ByteBuffer
import java.security.MessageDigest

/**
 * glide圆角
 */
class RoundedCornersTransform(
    val radius: Float,
    var isLeftTop: Boolean = true,
    var isRightTop: Boolean = true,
    var isLeftBottom: Boolean = true,
    var isRightBottom: Boolean = true
) : BitmapTransformation() {

    private val ID = "com.bumptech.glide.transformations.FillSpace"
    private val ID_BYTES = ID.toByteArray()

    override fun updateDiskCacheKey(messageDigest: MessageDigest) {
        messageDigest.update(ID_BYTES)
        val radiusData = ByteBuffer.allocate(4).putInt(radius.toInt()).array()
        messageDigest.update(radiusData)
    }

    override fun equals(other: Any?): Boolean {
        if (other is RoundedCornersTransform) {
            return radius == other.radius
        }
        return false
    }

    override fun hashCode(): Int {
        return Util.hashCode(ID.hashCode(), Util.hashCode(radius))
    }

    override fun transform(
        pool: BitmapPool,
        toTransform: Bitmap,
        outWidth: Int,
        outHeight: Int
    ): Bitmap {
        val bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight)
        return TransformationUtils.roundedCorners(pool, bitmap, radius.toInt())

//        return roundCrop(pool,toTransform)?:bitmap
    }

}